package com.lib.common.helper.api.upload;

import com.lib.common.BuildConfig;
import com.lib.common.entity.api.resp.UserInfo;
import com.lib.common.repository.user.UserInfoRepository;
import com.lib.framework.manager.ContextManager;
import com.tencent.cos.xml.CosXmlServiceConfig;
import com.tencent.cos.xml.common.Region;
import com.tencent.cos.xml.transfer.TransferConfig;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 文 件 名: QServiceCfg
 * 创 建 人: beyond
 * 创建日期: 2019-06-25 14:03
 * 邮   箱: beyond.wang@xiangwushuo.com
 * 文件功能：腾讯云上传配置类
 */
public class QServiceCfg {

    private String region = Region.AP_Shanghai.getRegion();
    public static String BUCKET = "gogowa-1255606258";

    public CosXmlServiceConfig serviceConfig;
    public TransferConfig transferConfig;
    private Map<String, String> mPartPathList;

    private static volatile QServiceCfg instance;

    public static QServiceCfg instance() {
        if (instance == null) {
            synchronized (QServiceCfg.class) {
                instance = new QServiceCfg();
            }
        }
        return instance;
    }

    private QServiceCfg() {
        serviceConfig = new CosXmlServiceConfig.Builder()
                .setRegion(region)
                .setDebuggable(BuildConfig.DEBUG)
                .setConnectionTimeout(10 * 1000) //设置连接超时时间，单位 毫秒 ，默认 45000ms
                .setSocketTimeout(10 * 1000)
                .builder();
        transferConfig = new TransferConfig.Builder()
                .setDividsionForCopy(5 * 1024 * 1024) // 是否启用分片复制的最小对象大小
                .setSliceSizeForCopy(5 * 1024 * 1024) //分片复制时的分片大小
                .setDivisionForUpload(2 * 1024 * 1024) // 是否启用分片上传的最小对象大小
                .setSliceSizeForUpload(1 * 1024 * 1024) //分片上传时的分片大小
                .build();

        mPartPathList = new LinkedHashMap<String, String>();

    }

    public String generateCosPath(String srcPath) {
        String userId = UserInfoRepository.getInstance().getUserId();
        return String.format("images/%s_%s", userId, "" + UUID.randomUUID().toString().replace("-", "") + srcPath.substring(srcPath.lastIndexOf(".")));
    }

    /**
     * 保存上传的本地地址和服务器地址的映射
     *
     * @param srcPath
     * @param cosPath
     */
    public void setPartPath(String srcPath, String cosPath) {
        mPartPathList.put(srcPath, cosPath);
    }

    public Map<String, String> getPartPathList() {
        return mPartPathList;
    }

    public void removeSrcPath(String srcPath) {
        mPartPathList.remove(srcPath);
    }

}
